package com.liuyk.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.BitmapShader;
import android.graphics.Shader;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

public class TouchImageView extends AppCompatImageView {
    private static final String TAG = "TouchImageView";

    public TouchImageView(Context context) {
        this(context, null);
    }

    public TouchImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TouchImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        //TileMode.CLAMP:用边缘色彩填充多余空间
        //TileMode.REPEAT:重复原图像来填充多余空间
        //TileMode.MIRROR:重复使用镜像模式的图像来填充多余空间
        BitmapShader bs = new BitmapShader(null, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//着色器
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "down");
                s(1f, 0.95f);
                return true;

            case MotionEvent.ACTION_UP:
                Log.e(TAG, "up");
                s(1f);
                return true;
        }

        return super.onTouchEvent(event);
    }

    private void s(float... scale) {
        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(this, "scaleX", scale);
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(this, "scaleY", scale);
        AnimatorSet as = new AnimatorSet();
        as.play(objectAnimatorX).with(objectAnimatorY);
        as.setDuration(200);
        as.start();
    }
}
